package com.controller;

import com.orm.Db;
import com.orm.ServiceCalledEntity;

import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * Created by lirance on 16/5/4.
 */
public class GetServiceCallList {
    int Gn = 30;

    public int getGn() {
        return Gn;
    }

    public void setGn(int gn) {
        Gn = gn;
    }

    public ArrayList GetServiceCallLists() throws Exception {
        String sql = "select * from sc" + Gn / 10;
        ArrayList<ServiceCalledEntity> serviceCalls = new ArrayList<>();

        ResultSet resultSet = Db.getInstance().executeQuery(sql);
        int i = 0;
        while (resultSet.next() /*&& i <= 1000*/) {
            i++;
            ServiceCalledEntity serCall = new ServiceCalledEntity();
            serCall.setuId(resultSet.getInt("uId"));
            serCall.setWsId(resultSet.getInt("wsId"));
           /* serCall.setDataSize(resultSet.getInt("dataSize"));
            serCall.setResCode(resultSet.getInt("resCode"));*/
            serCall.setRtt(resultSet.getInt("rtt"));
          /*  serCall.setResMessage(resultSet.getString("resMessage"));*/
            serCall.setFrequency(resultSet.getInt("frequency"));
            serviceCalls.add(serCall);
        }
        resultSet.close();
        return serviceCalls;
    }

    public ArrayList GetServiceCallListsByuId(int uid) throws Exception {
        String sql = "select * from sc" + Gn / 10 + " where uId = " + uid;
        ArrayList<ServiceCalledEntity> serviceCalls = new ArrayList<>();

        ResultSet resultSet = Db.getInstance().executeQuery(sql);
        int i = 0;
        while (resultSet.next() /*&& i <= 1000*/) {
            i++;
            ServiceCalledEntity serCall = new ServiceCalledEntity();
            serCall.setuId(resultSet.getInt("uId"));
            serCall.setWsId(resultSet.getInt("wsId"));/*
            serCall.setDataSize(resultSet.getInt("dataSize"));
            serCall.setResCode(resultSet.getInt("resCode"));*/
            serCall.setRtt(resultSet.getInt("rtt"));/*
            serCall.setResMessage(resultSet.getString("resMessage"));*/
            serCall.setFrequency(resultSet.getInt("frequency"));
            serviceCalls.add(serCall);
        }
        resultSet.close();
        return serviceCalls;
    }

    public ArrayList GetServiceCallListsBywsId(int wsid) throws Exception {
        String sql = "select * from sc" + Gn / 10 + " where wsId = " + wsid;
        ArrayList<ServiceCalledEntity> serviceCalls = new ArrayList<>();

        ResultSet resultSet = Db.getInstance().executeQuery(sql);
        int i = 0;
        while (resultSet.next() /*&& i <= 1000*/) {
            i++;
            ServiceCalledEntity serCall = new ServiceCalledEntity();
            serCall.setuId(resultSet.getInt("uId"));
            serCall.setWsId(resultSet.getInt("wsId"));/*
            serCall.setDataSize(resultSet.getInt("dataSize"));
            serCall.setResCode(resultSet.getInt("resCode"));*/
            serCall.setRtt(resultSet.getInt("rtt"));/*
            serCall.setResMessage(resultSet.getString("resMessage"));*/
            serCall.setFrequency(resultSet.getInt("frequency"));
            serviceCalls.add(serCall);
        }
        resultSet.close();
        return serviceCalls;
    }

    public ArrayList GetServiceCallListsByusId(int uid, int wsid) throws Exception {
        String sql = "select * from sc" + Gn / 10 + " where uId = " + uid + " and wsId = " + wsid;
        ArrayList<ServiceCalledEntity> serviceCalls = new ArrayList<>();

        ResultSet resultSet = Db.getInstance().executeQuery(sql);
        int i = 0;
        while (resultSet.next() /*&& i <= 1000*/) {
            i++;
            ServiceCalledEntity serCall = new ServiceCalledEntity();
            serCall.setuId(resultSet.getInt("uId"));
            serCall.setWsId(resultSet.getInt("wsId"));/*
            serCall.setDataSize(resultSet.getInt("dataSize"));
            serCall.setResCode(resultSet.getInt("resCode"));*/
            serCall.setRtt(resultSet.getInt("rtt"));/*
            serCall.setResMessage(resultSet.getString("resMessage"));*/
            serCall.setFrequency(resultSet.getInt("frequency"));
            serviceCalls.add(serCall);
        }
        resultSet.close();
        return serviceCalls;
    }

    public void updateClustNam(String clusteName, int wsId) throws Exception {
        Db.getInstance().executeUpdate("update webService set clusteNam=? where wsId=?", clusteName, wsId);
    }

    public void updateClustNum(String clusteNum, int wsId) throws Exception {
        Db.getInstance().executeUpdate("update webService set clusteNum=? where wsId=?", clusteNum, wsId);
    }

    /*public void updateClustName(String clusteName, int wsId) {
        Session session = this.getSessionFactory().openSession();
        session.beginTransaction();
        Query query = session.createQuery("update WebServiceEntity set clusteNam = '" + clusteName + "' where wsId = " + wsId);
        query.executeUpdate();
        session.getTransaction().commit();
        session.close();
    }*/
}
